package com.hxzy.course.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxzy.course.mapper.ArrangeCourseMapper;
import com.hxzy.course.mapper.CourseMapper;
import com.hxzy.course.mapper.StaffMapper;
import com.hxzy.course.mapper.TimeMapper;
import com.hxzy.course.pojo.ArrangeCourse;
import com.hxzy.course.pojo.Course;
import com.hxzy.course.pojo.Staff;
import com.hxzy.course.pojo.Time;
import com.hxzy.region.dao.RegionMapper;
import com.hxzy.region.mapper.ClassRoomMapper;
import com.hxzy.region.pojo.ClassRoom;
import com.hxzy.region.pojo.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ArrangeCourseService {

    @Autowired
    private ArrangeCourseMapper arrangeCourseMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private TimeMapper timeMapper;
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private ClassRoomMapper classRoomMapper;

    public Page<Map<String, Object>> getTuankeSchedule(Integer pageNum,Integer pageSize){
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<ArrangeCourse> queryWrapper = new QueryWrapper<>();
        //添加条件，查找的是团课课表
        queryWrapper.inSql("co_id","select co_id from course where co_type = '团课'");
        //课表和课程一张表，要保证课表相应字段非空
        queryWrapper.isNotNull("ti_id");
        queryWrapper.isNotNull("t_region_id");
        queryWrapper.isNotNull("cr_id");
        //保证课表状态在存在的
        queryWrapper.eq("ac_flag","1");
        //默认显示当天课程表
        queryWrapper.eq("date",dateFormat.format(date));
        //进行分页
        Page<ArrangeCourse> page = new Page<ArrangeCourse>(pageNum,pageSize);
        Page<ArrangeCourse> schedule = arrangeCourseMapper.selectPage(page,queryWrapper);
        List<Map<String,Object>> message = new ArrayList<>();
        //获取对应表的相关信息
        for (ArrangeCourse arrangecourse:schedule.getRecords()) {
            Map<String,Object> map = new HashMap<>();
            Course course = courseMapper.selectById(arrangecourse.getCoId());
            map.put("course",course);
            Time time = timeMapper.selectById(arrangecourse.getTiId());
            map.put("time",time);
            Staff staff = staffMapper.selectById(arrangecourse.getStaffId());
            map.put("teacher",staff);
            map.put("date",arrangecourse.getDate());
            Region region = regionMapper.selectById(arrangecourse.getTRegionId());
            map.put("location",region);
            ClassRoom classRoom = classRoomMapper.selectById(arrangecourse.getCrId());
            map.put("space",classRoom);
            map.put("arrangeId",arrangecourse.getArrangeId());
            message.add(map);
        }
        Page<Map<String, Object>> realSchedule = new Page<>(pageNum,pageSize);
        realSchedule.setTotal(schedule.getTotal());
        realSchedule.setSize(schedule.getSize());
        realSchedule.setCurrent(schedule.getCurrent());
        realSchedule.setPages(schedule.getPages());
        realSchedule.setRecords(message);
        return realSchedule;
    }

    public boolean addArrangeCourse(String courseId,String timeId,String date,String qLocation,String qSpace){
        ArrangeCourse arrangeCourse = new ArrangeCourse();
        int cid = Integer.valueOf(courseId);
        arrangeCourse.setCoId(cid);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("co_id",cid);
        queryWrapper.isNull("ti_id");
        queryWrapper.isNull("date");
        int teId = arrangeCourseMapper.selectOne(queryWrapper).getStaffId();
        arrangeCourse.setStaffId(teId);
        int tiId = Integer.valueOf(timeId);
        arrangeCourse.setTiId(tiId);
        int location = Integer.valueOf(qLocation);
        arrangeCourse.setTRegionId(location);
        int space = Integer.valueOf(qSpace);
        arrangeCourse.setCrId(space);
        arrangeCourse.setDate(date);
        int n = arrangeCourseMapper.insert(arrangeCourse);
        if(n > 0){
            return true;
        }
        return  false;
    }

    public boolean deleteSchedule(String delFlag,String delId){
        ArrangeCourse arrangeCourse = new ArrangeCourse();
        Integer id = Integer.valueOf(delId);
        arrangeCourse.setArrangeId(id);
        arrangeCourse.setAcFlag(0);
        int n = arrangeCourseMapper.updateById(arrangeCourse);
        return (n > 0)?true:false;
    }
}
